package com.sqfw.project.wpReport.service.impl;

import com.sqfw.framework.web.domain.AjaxResult;
import com.sqfw.project.system.domain.SysDept;
import com.sqfw.project.system.domain.SysUser;
import com.sqfw.project.system.mapper.SysDeptMapper;
import com.sqfw.project.system.mapper.SysUserMapper;
import com.sqfw.project.wp.domain.WpBackInfo;
import com.sqfw.project.wp.domain.WpHandleLog;
import com.sqfw.project.wp.domain.WpInfo;
import com.sqfw.project.wp.mapper.WpBackInfoMapper;
import com.sqfw.project.wp.mapper.WpHandleLogMapper;
import com.sqfw.project.wp.mapper.WpInfoMapper;
import com.sqfw.project.wpReport.domain.WpInfoEvaluate;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sqfw.project.wpReport.service.IWpInfoEvaluateService;

import java.util.List;

/**
 * 话务员流转工单评价表Service业务层处理
 * 
 * @author lx
 * @date 2023-07-24
 */
@Service
public class WpInfoEvaluateServiceImpl implements IWpInfoEvaluateService 
{

    @Autowired
    private WpBackInfoMapper wpBackInfoMapper;

    @Autowired
    private WpInfoMapper wpInfoMapper;

    @Autowired
    private WpHandleLogMapper wpHandleLogMapper;

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private SysDeptMapper sysDeptMapper;

    @Override
    public List<WpInfoEvaluate> getWpInfoEvaluateList(WpBackInfo wpBackInfo) {


        wpBackInfo.setState("督导处理");
        wpBackInfo.setNextState("坐席处理");
        List<WpInfoEvaluate> wpInfoEvaluates =  wpBackInfoMapper.evaluateSelectWpBackInfoList(wpBackInfo);

        for (WpInfoEvaluate wpInfoEvaluate : wpInfoEvaluates) {
            if ("勾选错误".equals(wpInfoEvaluate.getBackClass())){
                //15分
                wpInfoEvaluate.setDeductPoint(15);
                wpInfoEvaluate.setCheckPoint(15);
                wpInfoEvaluate.setFormatPoint(0);
                wpInfoEvaluate.setProcessPoint(0);
            }
            if ("格式错误".equals(wpInfoEvaluate.getBackClass())){
                //15分
                wpInfoEvaluate.setDeductPoint(15);
                wpInfoEvaluate.setFormatPoint(15);
                wpInfoEvaluate.setCheckPoint(0);
                wpInfoEvaluate.setProcessPoint(0);
            }
            if ("过程不明晰".equals(wpInfoEvaluate.getBackClass())){
                //30分
                wpInfoEvaluate.setDeductPoint(30);
                wpInfoEvaluate.setProcessPoint(30);
                wpInfoEvaluate.setFormatPoint(0);
                wpInfoEvaluate.setCheckPoint(0);
            }
            //左后得分为60（满分）减去扣除的分数
            if (wpInfoEvaluate.getDeductPoint()==null){
                wpInfoEvaluate.setDeductPoint(0);
            }
            if (wpInfoEvaluate.getCheckPoint()==null){
                wpInfoEvaluate.setCheckPoint(0);
            }
            if (wpInfoEvaluate.getFormatPoint()==null){
                wpInfoEvaluate.setFormatPoint(0);
            }
            if (wpInfoEvaluate.getCheckPoint()==null){
                wpInfoEvaluate.setCheckPoint(0);
            }
            if (wpInfoEvaluate.getProcessPoint()==null){
                wpInfoEvaluate.setProcessPoint(0);
            }
            wpInfoEvaluate.setCountPoint(60-wpInfoEvaluate.getDeductPoint());
        }

        return wpInfoEvaluates;
    }

    @Override
    public AjaxResult getWpInfoDetail(String wpid) {
        WpInfo wpInfo = wpInfoMapper.selectWpInfoByWpId(wpid);
        if (wpInfo == null) {
            return AjaxResult.error("该工单不存在");
        }
        if (!"0".equals(wpInfo.getDelFlag())) {
            return AjaxResult.error("该工单已删除");
        }
        WpHandleLog handleLog = new WpHandleLog();
        handleLog.setWpid(wpid);
        List<WpHandleLog> wpHandleLogs = wpHandleLogMapper.selectWpHandleLogList(handleLog);
        // 循环查询工单轨迹
        for (WpHandleLog wpHandleLog : wpHandleLogs) {
            if (StringUtils.isNotBlank(wpHandleLog.getOperator())) {
                SysUser user = userMapper.selectUserByCode(wpHandleLog.getOperator());
                if (user != null) {
                    wpHandleLog.setNickName(user.getNickName());
                }
            }
            if (StringUtils.isNotBlank(wpHandleLog.getDepartment())) {
                SysDept sysDept = sysDeptMapper.selectDeptById(Long.valueOf(wpHandleLog.getDepartment()));
                if (sysDept != null) {
                    wpHandleLog.setDepartmentName(sysDept.getDeptName());
                }
            }
        }
        wpInfo.setWpHandleLogs(wpHandleLogs);
        return AjaxResult.success(wpInfo);
    }
}
